/*
 * RacesItem.cpp
 *
 *  Created on: 2011-1-15
 *      Author: Administrator
 */

#include "RacesItem.h"
#include "ListBox.h"
#include "GuiFactory.h"

CRacesItem* CRacesItem::NewL(const CListBox* aOwner)
	{
	CRacesItem* self = CRacesItem::NewLC(aOwner);
    CleanupStack::Pop(self);
    return self;
	}

CRacesItem* CRacesItem::NewLC(const CListBox* aOwner)
	{
	CRacesItem* self = new(ELeave) CRacesItem();
    CleanupStack::PushL(self);
    self->ConstructL(aOwner);
    return self;
	}

CRacesItem::CRacesItem()
	{
	iBackImage = NULL;
	iLeftImage = NULL;
	iRightImage = NULL;
	iStateImage = NULL;
	iRace = NULL;
	}

CRacesItem::~CRacesItem()
	{
//	delete iBackImage;
//	iBackImage = NULL;
//	delete iLeftImage;
//	iLeftImage = NULL;
//	delete iRightImage;
//	iRightImage = NULL;
//	delete iStateImage;
//	iStateImage = NULL;
	CImageFactory::getInstance()->ReleaseImage(iBackImage);
	CImageFactory::getInstance()->ReleaseImage(iLeftImage);
	CImageFactory::getInstance()->ReleaseImage(iRightImage);
	CImageFactory::getInstance()->ReleaseImage(iStateImage);
	iRace = NULL;
	}

void CRacesItem::ConstructL(const CListBox* aOwner)
	{
	iOwner = aOwner;
	iStateImage = CGuiFactory::GetImageL(_L("state.png"));
	iBackImage = CGuiFactory::GetImageL(_L("team_black.png"));
	iTitleColor = KRgbWhite;
	iTextColor = KRgbYellow;
	ActivateL();
	}

void CRacesItem::SetLayout()
	{
	TInt height = iTitleFont->HeightInPixels() * 2 + 8;
	TInt width = 0;
	if (iLeftImage)
		height = iLeftImage->SizeInPixels().iHeight;
	else if (iStateImage)
		{
		height = iStateImage->SizeInPixels().iHeight;
		width = iStateImage->SizeInPixels().iWidth;
		}
	height += 20;
	if (iOwner->SelItemBackGroundBitmap())
	{
		TInt bmpHeight = iOwner->SelItemBackGroundBitmap()->SizeInPixels().iHeight;
		if ( bmpHeight > height)
			height = bmpHeight;
	}
	TInt wid = iOwner->Rect().Width() / 5;
	iLeftRect.SetRect(0,0,wid * 2,height);
	iMidRect.SetRect(wid * 2,0,wid * 3,height);
	if (width > iMidRect.Width())
		{
		iMidRect.SetWidth(width);
		iMidRect.Move(-(width - wid)/2,0);
		}
	iRightRect.SetRect(wid * 3,0,wid * 5,height);
	TRect rect(0,0,iOwner->Rect().Width() ,height);
	SetRect(rect);
	}

void CRacesItem::SetBackImage(CImage* aImage)
	{
	//delete iBackImage;
	CImageFactory::getInstance()->ReleaseImage(iBackImage);
	iBackImage = aImage;
	}

void CRacesItem::SetLeftBitmap(CImage* aImage)
	{
	//delete iLeftImage;
	CImageFactory::getInstance()->ReleaseImage(iLeftImage);
	iLeftImage = aImage;
	}

void CRacesItem::SetRightBitmap(CImage* aImage)
	{
	//delete iRightImage;
	CImageFactory::getInstance()->ReleaseImage(iRightImage);
	iRightImage = aImage;
	}

void CRacesItem::setRace(CRace* aRace)
	{
	if (aRace)
		{
		iRace = aRace;
//		delete iLeftImage;
//		iLeftImage = NULL;
		CImageFactory::getInstance()->ReleaseImage(iLeftImage);
		iLeftImage = CGuiFactory::GetTeamImageL(iRace->Team1());
//		delete iRightImage;
//		iRightImage = NULL;
		CImageFactory::getInstance()->ReleaseImage(iRightImage);
		iRightImage = CGuiFactory::GetTeamImageL(iRace->Team2());
		}
	}

CRace* CRacesItem::Race()
	{
	return iRace;
	}

void CRacesItem::Draw(CWindowGc& gc, const TRect& aRect) const
	{
	CBaseControl::Draw(gc, aRect);
	TPoint position = Position();
    if (iRace)
    	{
		TRect rect;
		TInt bitmapWidth = 0;
		TInt bitmapHeight = 0;
		//left
	    if (iLeftImage)
			{
			rect = iLeftRect;
			rect.Move(position);
			if (iBackImage)
				CGraphicsTool::DrawImage(gc, iBackImage->Bitmap(),iBackImage->MaskBitmap(), rect,CGraphicsContext::ELeft,rect.Width()/5);
			CGraphicsTool::DrawImage(gc, iLeftImage->Bitmap(),iLeftImage->MaskBitmap(), rect,CGraphicsContext::ELeft,rect.Width()/5);
			bitmapHeight = iLeftImage->SizeInPixels().iHeight;
			}
		if (iRace->Team1().Length() > 0)
			{
			gc.UseFont(iTitleFont);
			gc.SetPenColor(iTitleColor);
			rect = iLeftRect;
			rect.iTl.iY += (Rect().Height() - bitmapHeight) / 2;
			rect.iTl.iY += 2;
			rect.SetHeight(iTitleFont->HeightInPixels() + 4);
			rect.Move(position);
			TInt baseline = rect.Height() / 2 + iTitleFont->AscentInPixels() / 2;
			gc.DrawText(iRace->Team1(), rect, baseline, CGraphicsContext::ERight, 15);
			gc.DiscardFont();
			}
		if (iRace->Score1().Length() > 0)
			{
			gc.UseFont(iTextFont);
			gc.SetPenColor(iTextColor);
			rect = iLeftRect;
			rect.Move(0,iTitleFont->AscentInPixels()+ (Rect().Height() - bitmapHeight) / 2);
			rect.SetHeight(iTextFont->HeightInPixels() + 14);
			rect.Move(position);
			TInt baseline = rect.Height() / 2 + iTextFont->AscentInPixels() / 2;
			gc.DrawText(iRace->Score1(), rect, baseline, CGraphicsContext::ERight, 15);
			gc.DiscardFont();
			}
		//mid
		if (iStateImage)
			{
			rect = iMidRect;
			rect.Move(position);
			CGraphicsTool::DrawImage(gc, iStateImage->Bitmap(),iStateImage->MaskBitmap(), rect);
			bitmapHeight = iStateImage->SizeInPixels().iHeight;
			}
		if (iRace->StartTime().Length() > 0)
			{
			gc.UseFont(iTitleFont);
			gc.SetPenColor(iTitleColor);
			rect = iMidRect;
			rect.iTl.iY += (Rect().Height() - bitmapHeight) / 2;
			rect.iTl.iY += 2;
			rect.SetHeight(iTitleFont->HeightInPixels() + 4);
			rect.Move(position);
			TInt baseline = rect.Height() / 2 + iTitleFont->AscentInPixels() / 2;
			gc.DrawText(iRace->StartTime(), rect, baseline, CGraphicsContext::ECenter, -5);
			gc.DiscardFont();
			}
		if (iRace->State().Length() > 0)
			{
			gc.UseFont(iTextFont);
			gc.SetPenColor(iTextColor);
			rect = iMidRect;
			rect.Move(0,iTitleFont->AscentInPixels()+ (Rect().Height() - bitmapHeight) / 2);
			rect.SetHeight(iTextFont->HeightInPixels() + 4);
			rect.Move(position);
			TInt baseline = rect.Height() / 2 + iTextFont->AscentInPixels() / 2;
			gc.DrawText(iRace->State(), rect, baseline, CGraphicsContext::ECenter, -5);
			gc.DiscardFont();
			}
		//right
		if (iRightImage)
			{
			rect = iRightRect;
			rect.Move(position);
			if (iBackImage)
				CGraphicsTool::DrawImage(gc, iBackImage->Bitmap(),iBackImage->MaskBitmap(), rect,CGraphicsContext::ERight,rect.Width()/5);
			CGraphicsTool::DrawImage(gc, iRightImage->Bitmap(),iRightImage->MaskBitmap(), rect,CGraphicsContext::ERight,rect.Width()/5);
			bitmapHeight = iRightImage->SizeInPixels().iHeight;
			}
		if (iRace->Team2().Length() > 0)
			{
			gc.UseFont(iTitleFont);
			gc.SetPenColor(iTitleColor);
			rect = iRightRect;
			rect.iTl.iY += (Rect().Height() - bitmapHeight) / 2;
			rect.iTl.iY += 2;
			rect.SetHeight(iTitleFont->HeightInPixels() + 4);
			rect.Move(position);
			TInt baseline = rect.Height() / 2 + iTitleFont->AscentInPixels() / 2;
			gc.DrawText(iRace->Team2(), rect, baseline, CGraphicsContext::ELeft, 10);
			gc.DiscardFont();
			}
		if (iRace->Score2().Length() > 0)
			{
			gc.UseFont(iTextFont);
			gc.SetPenColor(iTextColor);
			rect = iRightRect;
			rect.Move(0,iTitleFont->AscentInPixels()+ (Rect().Height() - bitmapHeight) / 2);
			rect.SetHeight(iTextFont->HeightInPixels() + 14);
			rect.Move(position);
			TInt baseline = rect.Height() / 2 + iTextFont->AscentInPixels() / 2;
			gc.DrawText(iRace->Score2(), rect, baseline, CGraphicsContext::ELeft, 10);
			gc.DiscardFont();
			}
		}
    if (iOwner && iOwner->DrawLine())
    	{
		TInt gap = 0;
		if (iOwner->ScrollBar() && iOwner->ScrollBar()->Visible())
			gap = 6;
		CGraphicsTool::DrawLine(gc,TPoint(aRect.iTl.iX,aRect.iBr.iY - 1),TPoint(aRect.iBr.iX - gap,aRect.iBr.iY - 1),KRgbWhite);
    	}
	}
